package com.jk.myimkit.imkit.widget.provide;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.model.ProviderTag;
import com.jk.myimkit.imkit.model.UIMessage;
import com.jk.myimkit.imkit.widget.AsyncImageView;

import java.util.ArrayList;
import java.util.List;

import io.rong.message.PublicServiceMultiRichContentMessage;
import io.rong.message.RichContentItem;

@ProviderTag(
        messageContent = PublicServiceMultiRichContentMessage.class,
        showPortrait = false,
        centerInHorizontal = true,
        showSummaryWithName = false
)
public class PublicServiceMultiRichContentMessageProvider extends IContainerItemProvider.MessageProvider<PublicServiceMultiRichContentMessage> {
    public PublicServiceMultiRichContentMessageProvider() {
    }

    public void bindView(final View v, int position, PublicServiceMultiRichContentMessage content, UIMessage message) {
        PublicServiceMultiRichContentMessageProvider.ViewHolder vh = (PublicServiceMultiRichContentMessageProvider.ViewHolder)v.getTag();
        final ArrayList<RichContentItem> msgList = content.getMessages();
        if (msgList.size() > 0) {
            vh.tv.setText(((RichContentItem)msgList.get(0)).getTitle());
            vh.iv.setResource(((RichContentItem)msgList.get(0)).getImageUrl(), 0);
        }

//        int height = false;
        ViewGroup.LayoutParams params = v.getLayoutParams();
        PublicServiceMultiRichContentMessageProvider.PublicAccountMsgAdapter mAdapter = new PublicServiceMultiRichContentMessageProvider.PublicAccountMsgAdapter(v.getContext(), msgList);
        vh.lv.setAdapter(mAdapter);
        vh.lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RichContentItem item = (RichContentItem)msgList.get(position + 1);
                String url = item.getUrl();
                String action = "io.rong.imkit.intent.action.webview";
                Intent intent = new Intent(action);
                intent.setPackage(v.getContext().getPackageName());
                intent.putExtra("url", url);
                v.getContext().startActivity(intent);
            }
        });
        int height = this.getListViewHeight(vh.lv) + vh.height;
        params.height = height;
        v.setLayoutParams(params);
        v.requestLayout();
    }

    private int getListViewHeight(ListView list) {
        int totalHeight = 0;
        View item = null;
        ListAdapter adapter = list.getAdapter();

        for(int i = 0; i < adapter.getCount(); ++i) {
            item = adapter.getView(i, (View)null, list);
            item.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalHeight = totalHeight + item.getMeasuredHeight() + 2;
        }

        return totalHeight;
    }

    public Spannable getContentSummary(PublicServiceMultiRichContentMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, PublicServiceMultiRichContentMessage data) {
        List<RichContentItem> list = data.getMessages();
        return list.size() > 0 ? new SpannableString(((RichContentItem)data.getMessages().get(0)).getTitle()) : null;
    }

    public void onItemClick(View view, int position, PublicServiceMultiRichContentMessage content, UIMessage message) {
        if (content.getMessages().size() != 0) {
            String url = ((RichContentItem)content.getMessages().get(0)).getUrl();
            String action = "io.rong.imkit.intent.action.webview";
            Context context = view.getContext();
            Intent intent = new Intent(action);
            intent.setPackage(context.getPackageName());
            intent.putExtra("url", url);
            context.startActivity(intent);
        }
    }

    public View newView(Context context, ViewGroup group) {
        PublicServiceMultiRichContentMessageProvider.ViewHolder holder = new PublicServiceMultiRichContentMessageProvider.ViewHolder();
        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_public_service_multi_rich_content_message, (ViewGroup)null);
        holder.lv = (ListView)view.findViewById(R.id.rc_list);
        holder.iv = (AsyncImageView)view.findViewById(R.id.rc_img);
        holder.tv = (TextView)view.findViewById(R.id.rc_txt);
        view.measure(0, 0);
        holder.height = view.getMeasuredHeight();
        view.setTag(holder);
        return view;
    }

    private static class PublicAccountMsgAdapter extends BaseAdapter {
        LayoutInflater inflater;
        ArrayList<RichContentItem> itemList;
        int itemCount;

        public PublicAccountMsgAdapter(Context context, ArrayList<RichContentItem> msgList) {
            this.inflater = LayoutInflater.from(context);
            this.itemList = new ArrayList();
            this.itemList.addAll(msgList);
            this.itemCount = msgList.size() - 1;
        }

        public int getCount() {
            return this.itemCount;
        }

        public RichContentItem getItem(int position) {
            return this.itemList.size() == 0 ? null : (RichContentItem)this.itemList.get(position + 1);
        }

        public long getItemId(int position) {
            return 0L;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = this.inflater.inflate(R.layout.rc_item_public_service_message, (ViewGroup)null);
            AsyncImageView iv = (AsyncImageView)convertView.findViewById(R.id.rc_img);
            TextView tv = (TextView)convertView.findViewById(R.id.rc_txt);
            if (this.itemList.size() == 0) {
                return null;
            } else {
                String title = ((RichContentItem)this.itemList.get(position + 1)).getTitle();
                if (title != null) {
                    tv.setText(title);
                }

                iv.setResource(((RichContentItem)this.itemList.get(position + 1)).getImageUrl(), 0);
                return convertView;
            }
        }
    }

    protected static class ViewHolder {
        public int height;
        public TextView tv;
        public AsyncImageView iv;
        public ListView lv;

        protected ViewHolder() {
        }
    }
}
